<template>
    <Layout>
        <Form ref="form1" :model="form1" :label-width="120">
            <Row v-show="!switchFlag" class="query-form" style="padding-top: 15px">
                <Col span="8">
                    <FormItem label="姓名：" prop="userName">
                        <Input class="query-form-el" type="text" v-model="form1.userName"></Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="身份证：" prop="idCard">
                        <Input class="query-form-el" type="text" v-model="form1.idCard"></Input>
                    </FormItem>
                </Col>
                <Col span="8">

                    <Button @click="reset" class="ivu-btn-reset">重置</Button>
                    <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
            </Row>

        </Form>

        <Table ref='table' :loading="loading" size="small" :height="tableHeight" no-data-text="没有查询到相关数据!"
               border :columns="columns1" :data="dataTable">
        </Table>

        <Row style="margin-top:10px;">
            <Col span="24">
                <Page style='float: right' :total="pagedata.total" :page-size="pagedata.pageSize"
                      @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray"
                      show-total show-sizer/>
            </Col>
        </Row>
    </layout>
</template>

<script>
    import {Api} from '../../../../api/index.js'
    import {sessionStorage} from '../../../../common/cacheUtils.js';

    export default {
        data() {
            return {
                form1: {},
				switchFlag:false,
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
                columns1: [
                    {
                        title: "序号",
                        width: 80,
                        align: "center",
                        fixed: 'left',
                        render: (h, params) => {
                            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
                        }
                    },
                    {
                        title: '姓名',
                        width: 150,
                        align: 'center',
                        key: 'userName',
                    },
                    {
                        title: '所属部门',
                        width: 150,
                        align: 'center',
                        key: 'orgName',
                    },
                    {
                        title: '职务',
                        width: 150,
                        align: 'center',
                        key: 'position',
                    },
                    {
                        title: '性别',
                        width: 150,
                        align: 'center',
                        key: 'gender',
                    },
                    {
                        title: '出生日期',
                        width: 150,
                        align: 'center',
                        key: 'birthDay',
                        render: (h, params) => {
                            return h('div', [
                                (this.formatDate(new Date(params.row.birthDay), 'yyyy-MM-dd hh:ss'))
                            ]);
                        }
                    },
                    {
                        title: '民族',
                        width: 150,
                        align: 'center',
                        key: 'race',
                    },
                    {
                        title: '身份证',
                        width: 150,
                        align: 'center',
                        key: 'idCard',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                    },
                                    on: {
                                        click: () => {
                                            this.save(params.row);
                                        }
                                    }
                                }, '选择')
                            ]);
                        }
                    }

                ],
                loading: false, tableHeight: 450,
                dataTable: [],
            }
        },
        mounted: function () {
            this.tableHeight = window.innerHeight * 0.85 - this.$refs.table.$el.offsetTop - 73;
            // this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            this.query();
        },
        methods: {
            pageChange(pageNum) {
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },

            copyArray(arr) {
                return JSON.parse(JSON.stringify(arr));
            },


            save(row) {
                this.$emit('getSelectData', row);
            },

            query() {
                this.loading = true;
                let data = {
                    startPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    orgId: sessionStorage('userId').getObject('userId').orgInfo.parentId
                }
                data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));
                let url = Api.queryEqCarDriver;
                this.$http.get(url, {params: data}).then((res) => {
                    if (res.data.status == 200) {
                        this.dataTable = res.data.data.data;
                        this.pagedata.total = res.data.data.totalSize;
                        this.loading = false;
                    } else {
                        this.$Message.error("数据获取失败")
                    }
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset() {
                this.form1 = {}
				this.query()
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
        },
    }
</script>
